

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/2.jpg">
  <link rel="icon" type="image/png" href="/img/2.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="K">
  <meta name="keywords" content="">
  <title>docker-machine - K</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>かい</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/1.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-09 14:00" pubdate>
        2020年12月9日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.4k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      40
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">docker-machine</h1>
            
            <div class="markdown-body" id="post-body">
              <h2 id="docker-machine"><a href="#docker-machine" class="headerlink" title="docker-machine"></a>docker-machine</h2><ul>
<li>是一个简化docker安装的命令行工具。可以让主机一瞬间安装很多容器，并且可以远程进行管理</li>
</ul>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><ul>
<li><p><a target="_blank" rel="noopener" href="https://docs.docker.com/search/?q=machine">https://docs.docker.com/search/?q=machine</a> 可以查看怎样安装</p>
<blockquote>
<p>复制官网的命令下载完即可完成安装</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@bogon ~]</span># base=https:<span class="hljs-comment">//github.com/docker/machine/releases/download/v0.16.0 &amp;&amp;  curl -L $base/docker-machine-$(uname -s)-$(uname -m) &gt;/tmp/docker-machine &amp;&amp;  sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &amp;&amp;   chmod +x /usr/local/bin/docker-machine</span>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  <span class="hljs-number">0</span>     <span class="hljs-number">0</span>    <span class="hljs-number">0</span>     <span class="hljs-number">0</span>    <span class="hljs-number">0</span>     <span class="hljs-number">0</span>      <span class="hljs-number">0</span>      <span class="hljs-number">0</span> --:--:--  <span class="hljs-number">0</span>:<span class="hljs-number">00</span>:<span class="hljs-number">05</span> --:--:--     <span class="hljs-number">0</span>
<span class="hljs-number">100</span> <span class="hljs-number">26.8</span>M  <span class="hljs-number">100</span> <span class="hljs-number">26.8</span>M    <span class="hljs-number">0</span>     <span class="hljs-number">0</span>  <span class="hljs-number">46349</span>      <span class="hljs-number">0</span>  <span class="hljs-number">0</span>:<span class="hljs-number">10</span>:<span class="hljs-number">07</span>  <span class="hljs-number">0</span>:<span class="hljs-number">10</span>:<span class="hljs-number">07</span> --:--:--  <span class="hljs-number">108</span>k
</code></pre>
<blockquote>
<p>查看版本号</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@bogon ~]</span># docker-machine -v
docker-machine version <span class="hljs-number">0.16</span><span class="hljs-number">.0</span>, build <span class="hljs-number">702</span>c267f</code></pre>
<blockquote>
<p>官方让将一下代码复制到脚本中</p>
</blockquote>
<pre><code class="hljs pgsql">[root@bogon ~]# cd /etc/bash_completion.d/
[root@bogon bash_completion.d]# vi down.sh

base=https://raw.githubusercontent.com/docker/machine/v0<span class="hljs-number">.16</span><span class="hljs-number">.0</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> docker-machine-prompt.bash docker-machine-<span class="hljs-keyword">wrapper</span>.bash docker-machine.bash
<span class="hljs-keyword">do</span>
  sudo wget &quot;$base/contrib/completion/bash/$&#123;i&#125;&quot; -P /etc/bash_completion.d
done
[root@bogon bash_completion.d]# source down.sh 
</code></pre>
<blockquote>
<p>上面是正常的顺序，但是需要连接谷歌</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>不连接谷歌这样做</p>
</blockquote>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://github.com/docker/machine">https://github.com/docker/machine</a>    gitub这个网站<br><img src="/images/docker/machine1.png" srcset="/img/loading.gif"><br><img src="/images/docker/machine2.png" srcset="/img/loading.gif"></p>
</li>
<li><p>复制这个网站的三个脚本代码 <a target="_blank" rel="noopener" href="https://github.com/docker/machine/tree/master/contrib/completion/bash">https://github.com/docker/machine/tree/master/contrib/completion/bash</a><br><img src="/images/docker/machine3.png" srcset="/img/loading.gif"></p>
<pre><code class="hljs pgsql"># 复制代码到这个目录里就可以了
[root@bogon bash_completion.d]# vi docker-machine-prompt.bash
[root@bogon bash_completion.d]# vi docker-machine-<span class="hljs-keyword">wrapper</span>.bash
[root@bogon bash_completion.d]# vi docker-machine.bash
[root@bogon bash_completion.d]# pwd
/etc/bash_completion.d</code></pre>
</li>
<li><p>设置docker-machine的环境提示</p>
<blockquote>
<p>根据官网中的提示，要启用docker-machine外壳程序提示，$(__docker_machine_ps1)请PS1在中添加 设置~/.bashrc。</p>
</blockquote>
<pre><code class="hljs ini"><span class="hljs-comment">#  家目录</span>
<span class="hljs-section">[root@bogon ~]</span><span class="hljs-comment"># vi .bashrc </span>
<span class="hljs-comment"># 最后添加</span>
<span class="hljs-attr">PS1</span>=<span class="hljs-string">&#x27;[\u@\h \W$(__docker_machine_ps1)]\$ &#x27;</span>
</code></pre>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@bogon ~]</span># cd -
/etc/bash_completion.d

<span class="hljs-string">[root@bogon bash_completion.d]</span># cp -p docker-machine.bash docker-machine

<span class="hljs-string">[root@bogon ~]</span># docker-machine --version 
docker-machine version <span class="hljs-number">0.16</span><span class="hljs-number">.0</span>, build <span class="hljs-number">702</span>c267f
</code></pre>

</li>
</ul>
<h2 id="远程安装docker"><a href="#远程安装docker" class="headerlink" title="远程安装docker"></a>远程安装docker</h2><ul>
<li><p>192.168.100.212 对另外两台主机设置免密登录</p>
<table>
<thead>
<tr>
<th>192.168.100.212</th>
<th>docker-machine</th>
</tr>
</thead>
<tbody><tr>
<td>192.168.100.202</td>
<td>host1</td>
</tr>
<tr>
<td>192.168.100.205</td>
<td>host2</td>
</tr>
</tbody></table>
<blockquote>
<p>三台主机都需要修改</p>
</blockquote>
<pre><code class="hljs coffeescript"><span class="hljs-comment"># 注意: 三台主机都做 都做</span>
[root@bogon ~]<span class="hljs-comment"># vim /etc/ssh/sshd_config </span>
<span class="hljs-comment"># 将以下取消注释，表示允许root权限登录操作</span>
PermitRootLogin <span class="hljs-literal">yes</span> 
[root@bogon ~]<span class="hljs-comment"># systemctl restart sshd</span>
[root@bogon ~]<span class="hljs-comment"># vim /etc/sudoers</span>
<span class="hljs-comment"># 将以下内容去掉!符号</span>
    <span class="hljs-comment"># 提权的意思 保持权限连接ssh</span>
Defaults   !visiblepw</code></pre>

<blockquote>
<p>生成秘钥</p>
</blockquote>
<pre><code class="hljs gherkin"><span class="hljs-comment"># 生成秘钥</span>
[root<span class="hljs-meta">@bogon</span> ~]<span class="hljs-comment"># ssh-keygen </span>
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bGNIS9qQj3TO4TqW6v2CaEFpFk0NCCnVyCfhYy9cWNc root<span class="hljs-meta">@bogon</span>
The key&#x27;s randomart image is:
+---[RSA 2048]----+
|<span class="hljs-string">o+**+ ..         </span>|
|<span class="hljs-string">o+++o+  E        </span>|
|<span class="hljs-string">. Bo= =          </span>|
|<span class="hljs-string"> B = &amp; =         </span>|
|<span class="hljs-string">+ o + O S        </span>|
|<span class="hljs-string"> . . o o .       </span>|
|<span class="hljs-string"> ...=            </span>|
|<span class="hljs-string">...+..           </span>|
|<span class="hljs-string">..o .o.          </span>|
+----[SHA256]-----+</code></pre>
<blockquote>
<p>对202(host1)进行免密登录</p>
</blockquote>
<pre><code class="hljs applescript"><span class="hljs-comment"># 对 204进行免密登录</span>
[root@bogon ~]<span class="hljs-comment"># ssh-copy-id -i root@192.168.100.202</span>
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: Source <span class="hljs-keyword">of</span> key(s) <span class="hljs-keyword">to</span> be installed: <span class="hljs-string">&quot;/root/.ssh/id_rsa.pub&quot;</span>
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: attempting <span class="hljs-keyword">to</span> <span class="hljs-built_in">log</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">with</span> <span class="hljs-keyword">the</span> new key(s), <span class="hljs-keyword">to</span> filter out any <span class="hljs-keyword">that</span> are already installed
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: <span class="hljs-number">1</span> key(s) remain <span class="hljs-keyword">to</span> be installed <span class="hljs-comment">-- if you are prompted now it is to install the new keys</span>
root@<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.202</span>&#x27;s password: 

Number <span class="hljs-keyword">of</span> key(s) added: <span class="hljs-number">1</span>

Now <span class="hljs-keyword">try</span> logging <span class="hljs-keyword">into</span> <span class="hljs-keyword">the</span> machine, <span class="hljs-keyword">with</span>:   <span class="hljs-string">&quot;ssh &#x27;root@192.168.100.204&#x27;&quot;</span>
<span class="hljs-keyword">and</span> check <span class="hljs-keyword">to</span> make sure <span class="hljs-keyword">that</span> only <span class="hljs-keyword">the</span> key(s) you wanted were added.</code></pre>
<blockquote>
<p>对205(host2)进行免密登录</p>
</blockquote>
<pre><code class="hljs applescript"><span class="hljs-comment"># 对205进行免密登录</span>
[root@bogon ~]<span class="hljs-comment"># ssh-copy-id -i root@192.168.100.205</span>
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: Source <span class="hljs-keyword">of</span> key(s) <span class="hljs-keyword">to</span> be installed: <span class="hljs-string">&quot;/root/.ssh/id_rsa.pub&quot;</span>
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: attempting <span class="hljs-keyword">to</span> <span class="hljs-built_in">log</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">with</span> <span class="hljs-keyword">the</span> new key(s), <span class="hljs-keyword">to</span> filter out any <span class="hljs-keyword">that</span> are already installed
/usr/bin/ssh-<span class="hljs-keyword">copy</span>-<span class="hljs-built_in">id</span>: INFO: <span class="hljs-number">1</span> key(s) remain <span class="hljs-keyword">to</span> be installed <span class="hljs-comment">-- if you are prompted now it is to install the new keys</span>
root@<span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.205</span>&#x27;s password: 

Number <span class="hljs-keyword">of</span> key(s) added: <span class="hljs-number">1</span>

Now <span class="hljs-keyword">try</span> logging <span class="hljs-keyword">into</span> <span class="hljs-keyword">the</span> machine, <span class="hljs-keyword">with</span>:   <span class="hljs-string">&quot;ssh &#x27;root@192.168.100.205&#x27;&quot;</span>
<span class="hljs-keyword">and</span> check <span class="hljs-keyword">to</span> make sure <span class="hljs-keyword">that</span> only <span class="hljs-keyword">the</span> key(s) you wanted were added.
</code></pre></li>
<li><p>212远程安装docker</p>
<blockquote>
<p>远程安装需要指定驱动器 --driver<br>--generic-ip-address 是指定那台主机要安装docker</p>
</blockquote>
<ul>
<li>linux要使用 generic</li>
<li>mac，windows 参考官网 <a target="_blank" rel="noopener" href="https://docs.docker.com/machine/drivers/azure/">https://docs.docker.com/machine/drivers/azure/</a><blockquote>
<p>为202安装docker 起名为 host1</p>
</blockquote>
<pre><code class="hljs routeros"><span class="hljs-comment"># 为202安装docker 起名为 host1</span>
[root@localhost ~]# docker-machine create --driver generic --generic-ip-address 192.168.100.202 host1
Running pre-create checks<span class="hljs-built_in">..</span>.
Creating machine<span class="hljs-built_in">..</span>.
(host1) <span class="hljs-literal">No</span> SSH key specified. Assuming an existing key at the<span class="hljs-built_in"> default </span>location.
Waiting <span class="hljs-keyword">for</span> machine <span class="hljs-keyword">to</span> be running, this may take a few minutes<span class="hljs-built_in">..</span>.
Detecting operating<span class="hljs-built_in"> system </span>of created instance<span class="hljs-built_in">..</span>.
Waiting <span class="hljs-keyword">for</span> SSH <span class="hljs-keyword">to</span> be available<span class="hljs-built_in">..</span>.
Detecting the provisioner<span class="hljs-built_in">..</span>.
Provisioning with centos<span class="hljs-built_in">..</span>.
Copying certs <span class="hljs-keyword">to</span> the local machine directory<span class="hljs-built_in">..</span>.
Copying certs <span class="hljs-keyword">to</span> the remote machine<span class="hljs-built_in">..</span>.
Setting Docker configuration on the remote daemon<span class="hljs-built_in">..</span>.
Checking<span class="hljs-built_in"> connection </span><span class="hljs-keyword">to</span> Docker<span class="hljs-built_in">..</span>.
Docker is up <span class="hljs-keyword">and</span> running!
<span class="hljs-keyword">To</span> see how <span class="hljs-keyword">to</span> connect your Docker<span class="hljs-built_in"> Client </span><span class="hljs-keyword">to</span> the Docker Engine running on this virtual machine, run: docker-machine env host1
</code></pre>
<blockquote>
<p>为205安装docker 起名为 host2</p>
</blockquote>
<pre><code class="hljs routeros">[root@localhost ~]# docker-machine create --driver generic --generic-ip-address 192.168.100.205 host2
Running pre-create checks<span class="hljs-built_in">..</span>.
Creating machine<span class="hljs-built_in">..</span>.
(host2) <span class="hljs-literal">No</span> SSH key specified. Assuming an existing key at the<span class="hljs-built_in"> default </span>location.
Waiting <span class="hljs-keyword">for</span> machine <span class="hljs-keyword">to</span> be running, this may take a few minutes<span class="hljs-built_in">..</span>.
Detecting operating<span class="hljs-built_in"> system </span>of created instance<span class="hljs-built_in">..</span>.
Waiting <span class="hljs-keyword">for</span> SSH <span class="hljs-keyword">to</span> be available<span class="hljs-built_in">..</span>.
Detecting the provisioner<span class="hljs-built_in">..</span>.
Provisioning with centos<span class="hljs-built_in">..</span>.
Copying certs <span class="hljs-keyword">to</span> the local machine directory<span class="hljs-built_in">..</span>.
Copying certs <span class="hljs-keyword">to</span> the remote machine<span class="hljs-built_in">..</span>.
Setting Docker configuration on the remote daemon<span class="hljs-built_in">..</span>.
Checking<span class="hljs-built_in"> connection </span><span class="hljs-keyword">to</span> Docker<span class="hljs-built_in">..</span>.
Docker is up <span class="hljs-keyword">and</span> running!
<span class="hljs-keyword">To</span> see how <span class="hljs-keyword">to</span> connect your Docker<span class="hljs-built_in"> Client </span><span class="hljs-keyword">to</span> the Docker Engine running on this virtual machine, run: docker-machine env host2
</code></pre>
<blockquote>
<p>查看状态</p>
</blockquote>
<pre><code class="hljs awk">[root@localhost ~]<span class="hljs-comment"># docker-machine ls</span>
NAME    ACTIVE   DRIVER    STATE     URL                          SWARM   DOCKER     ERRORS
host1   -        generic   Running   tcp:<span class="hljs-regexp">//</span><span class="hljs-number">192.168</span>.<span class="hljs-number">100.202</span>:<span class="hljs-number">2376</span>           v20.<span class="hljs-number">10.0</span>   
host2   -        generic   Running   tcp:<span class="hljs-regexp">//</span><span class="hljs-number">192.168</span>.<span class="hljs-number">100.205</span>:<span class="hljs-number">2376</span>           v20.<span class="hljs-number">10.0</span>   
</code></pre>

</li>
</ul>
</li>
</ul>
<h2 id="下面根据实验来使用docker-machine的命令"><a href="#下面根据实验来使用docker-machine的命令" class="headerlink" title="下面根据实验来使用docker-machine的命令"></a>下面根据实验来使用docker-machine的命令</h2><p> <img src="/images/docker/xm.jpg" srcset="/img/loading.gif"></p>
<blockquote>
<p>下载镜像</p>
</blockquote>
<ul>
<li><p>host1</p>
<pre><code class="hljs routeros">[root@localhost ~]# docker-machine env host1
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_TLS_VERIFY</span>=<span class="hljs-string">&quot;1&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_HOST</span>=<span class="hljs-string">&quot;tcp://192.168.100.202:2376&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_CERT_PATH</span>=<span class="hljs-string">&quot;/root/.docker/machine/machines/host1&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_MACHINE_NAME</span>=<span class="hljs-string">&quot;host1&quot;</span>
<span class="hljs-comment"># Run this command to configure your shell: </span>
<span class="hljs-comment"># eval $(docker-machine env host1)</span></code></pre></li>
<li><p>根据上面的提示运行eval $(docker-machine env host1) 可以进入host1环境中</p>
<pre><code class="hljs ldif"><span class="hljs-comment"># 先执行bash的原因是</span>
  <span class="hljs-comment"># 如果exit就直接把当前主机的bash退出了</span>
[root@localhost ~]<span class="hljs-comment"># bash</span>
[root@localhost ~]<span class="hljs-comment"># eval $(docker-machine env host1)</span>
<span class="hljs-comment"># 下载镜像</span>
[root@localhost ~ [host1]]<span class="hljs-comment"># docker pull httpd</span>
<span class="hljs-attribute">Using default tag</span>: latest
<span class="hljs-attribute">latest</span>: Pulling from library/httpd
<span class="hljs-attribute">852e50cd189d</span>: Pull complete 
<span class="hljs-attribute">67d51c33d390</span>: Pull complete 
<span class="hljs-attribute">b0ad2a3b9567</span>: Pull complete 
<span class="hljs-attribute">136f1f71f30c</span>: Pull complete 
<span class="hljs-attribute">01f8ace29294</span>: Pull complete 
<span class="hljs-attribute">Digest</span>: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456
<span class="hljs-attribute">Status</span>: Downloaded newer image for httpd:latest
<span class="hljs-attribute">docker.io/library/httpd:latest</span></code></pre></li>
<li><p>host2</p>
<pre><code class="hljs routeros">[root@localhost ~]# docker-machine env host2
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_TLS_VERIFY</span>=<span class="hljs-string">&quot;1&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_HOST</span>=<span class="hljs-string">&quot;tcp://192.168.100.205:2376&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_CERT_PATH</span>=<span class="hljs-string">&quot;/root/.docker/machine/machines/host2&quot;</span>
<span class="hljs-builtin-name">export</span> <span class="hljs-attribute">DOCKER_MACHINE_NAME</span>=<span class="hljs-string">&quot;host2&quot;</span>
<span class="hljs-comment"># Run this command to configure your shell: </span>
<span class="hljs-comment"># eval $(docker-machine env host2)</span>
    # 根据上面的提示运行eval $(docker-machine env host1) 可以进入host1环境中
    # 先执行bash的原因是
      # 如果exit就直接把当前主机的bash退出了
[root@localhost ~]# bash
[root@localhost ~]# eval $(docker-machine env host2)
<span class="hljs-comment"># 下载镜像</span>
[root@localhost ~ [host2]]# docker pull httpd
Using<span class="hljs-built_in"> default </span>tag: latest
latest: Pulling <span class="hljs-keyword">from</span> library/httpd
852e50cd189d: Pull complete 
67d51c33d390: Pull complete 
b0ad2a3b9567: Pull complete 
136f1f71f30c: Pull complete 
01f8ace29294: Pull complete 
Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456
Status: Downloaded newer image <span class="hljs-keyword">for</span> httpd:latest
docker.io/library/httpd:latest
</code></pre>
<ul>
<li><p>启动和正常启动一样</p>
</li>
<li><p>映射挂载目录</p>
</li>
<li><p>将docker-machine主机上的目录映射到被管理端的容器中</p>
<blockquote>
<p>安装支持docker-machine使用mount命令的工具</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta">#安装支持docker-machine使用mount命令的工具</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># yum install -y epel-release</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># yum -y install fuse-sshfs</span>

<span class="hljs-meta"># docker-machine创建目录</span>
[root<span class="hljs-symbol">@localhost</span> ~ [host1]]<span class="hljs-meta"># mkdir htdocs</span></code></pre>
<blockquote>
<p>在两个host中创建htdocs目录</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># 在两个host中创建htdocs目录</span>
<span class="hljs-meta"># machine的目录必须为空，要不然挂不进去</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine ssh host1 mkdir /htdocs</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine ssh host2 mkdir /htdocs</span></code></pre>
<blockquote>
<p>将machine的目录挂载到host1和host2</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta"># </span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine mount host1:/root/htdocs htdocs/</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine mount host2:/root/htdocs htdocs/</span></code></pre>
<blockquote>
<p>启动两个容器</p>
</blockquote>
<pre><code class="hljs autoit"><span class="hljs-meta">## </span>
<span class="hljs-meta">#进入 host1启动httpd</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># eval $(docker-machine env host1)</span>
[root<span class="hljs-symbol">@localhost</span> ~ [host1]]<span class="hljs-meta"># </span>
[root<span class="hljs-symbol">@localhost</span> ~ [host1]]<span class="hljs-meta"># docker run -itd --name httpd -p 80:80 --volume /root/htdocs:/usr/local/apache2/htdocs httpd</span>
<span class="hljs-number">4014</span>c56e36f6fda2800953d468efd634bb7c08b7672517427dc3aea63e10f56b
<span class="hljs-meta">#进入 host2启动httpd</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># eval $(docker-machine env host2)</span>
[root<span class="hljs-symbol">@localhost</span> ~ [host2]]<span class="hljs-meta"># docker run -itd --name httpd -p 80:80 --volume /root/htdocs:/usr/local/apache2/htdocs httpd</span>
<span class="hljs-number">06</span>df25f57fa1b340cadc0429e6345247ef294b00c1a7042655c1b73a6006003b</code></pre>
<blockquote>
<p>在本地的 htdocs 写入文件</p>
</blockquote>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~ [host1]]<span class="hljs-meta"># echo <span class="hljs-string">&#x27;李明凯&#x27;</span> &gt; htdocs/index.html</span></code></pre>
<blockquote>
<p>访问</p>
</blockquote>
<pre><code class="hljs angelscript"><span class="hljs-string">[root@localhost htdocs]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.205</span>
李明凯
<span class="hljs-string">[root@localhost htdocs]</span># curl <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.202</span>
李明凯
</code></pre>

</li>
</ul>
</li>
</ul>
<h2 id="其他命令"><a href="#其他命令" class="headerlink" title="其他命令"></a>其他命令</h2><p> 查看主机配置</p>
<ul>
<li><p>docker-machine inspect host</p>
<pre><code class="hljs autoit">[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine inspect host1</span>
&#123;
    <span class="hljs-string">&quot;ConfigVersion&quot;</span>: <span class="hljs-number">3</span>,
    <span class="hljs-string">&quot;Driver&quot;</span>: &#123;
        <span class="hljs-string">&quot;IPAddress&quot;</span>: <span class="hljs-string">&quot;192.168.100.202&quot;</span>,
<span class="hljs-meta"># 列举一点</span></code></pre>
<p>查看被管理端ip</p>
<pre><code class="hljs accesslog"><span class="hljs-string">[root@localhost ~]</span># docker-machine ip host1 host2
<span class="hljs-number">192.168.100.202</span>
<span class="hljs-number">192.168.100.205</span>
</code></pre>
<p>被管理端之间的文件传输</p>
<pre><code class="hljs gradle"># host1主机内
[root@localhost ~]# echo <span class="hljs-string">&quot;This file in the host1&quot;</span> &gt; <span class="hljs-regexp">/tmp/</span>host1

# docker-machine
[root@localhost ~]# docker-machine scp host1:<span class="hljs-regexp">/tmp/</span>host1 host2:<span class="hljs-regexp">/tmp/</span>bb
[root@localhost ~]# docker-machine ssh host2 cat <span class="hljs-regexp">/tmp/</span>bb
<span class="hljs-keyword">This</span> <span class="hljs-keyword">file</span> in the host1
</code></pre>
<p>批量升级docker版本</p>
<pre><code class="hljs routeros">[root@localhost ~]# docker-machine<span class="hljs-built_in"> upgrade </span>host1 host2
</code></pre>
<p>远程主机切换远程主机</p>
<pre><code class="hljs reasonml"><span class="hljs-literal">[<span class="hljs-identifier">root</span>@<span class="hljs-identifier">localhost</span> ~ [<span class="hljs-identifier">host1</span>]</span>]# eval <span class="hljs-constructor">$(<span class="hljs-params">docker</span>-<span class="hljs-params">machine</span> <span class="hljs-params">env</span> <span class="hljs-params">host2</span>)</span></code></pre>
<p>ssh连接远程主机执行命令</p>
<pre><code class="hljs angelscript"># 查看了host1的网卡
<span class="hljs-string">[root@localhost ~]</span># docker-machine ssh host1 ifconfig
docker0: flags=<span class="hljs-number">4163</span>&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu <span class="hljs-number">1500</span>
        inet <span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>  netmask <span class="hljs-number">255.255</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>  broadcast <span class="hljs-number">172.17</span><span class="hljs-number">.255</span><span class="hljs-number">.255</span>
        inet6 fe80::<span class="hljs-number">42</span>:<span class="hljs-number">34f</span>f:fef6:<span class="hljs-number">6</span>c21  prefixlen <span class="hljs-number">64</span>  scopeid <span class="hljs-number">0x20</span>&lt;link&gt;
        ether <span class="hljs-number">02</span>:<span class="hljs-number">42</span>:<span class="hljs-number">34</span>:f6:<span class="hljs-number">6</span>c:<span class="hljs-number">21</span>  txqueuelen <span class="hljs-number">0</span>  (Ethernet)
        RX packets <span class="hljs-number">12</span>  bytes <span class="hljs-number">1090</span> (<span class="hljs-number">1.0</span> KiB)
        RX errors <span class="hljs-number">0</span>  dropped <span class="hljs-number">0</span>  overruns <span class="hljs-number">0</span>  frame <span class="hljs-number">0</span>
        TX packets <span class="hljs-number">29</span>  bytes <span class="hljs-number">2902</span> (<span class="hljs-number">2.8</span> KiB)
        TX errors <span class="hljs-number">0</span>  dropped <span class="hljs-number">0</span> overruns <span class="hljs-number">0</span>  carrier <span class="hljs-number">0</span>  collisions <span class="hljs-number">0</span>

ens33: flags=<span class="hljs-number">4163</span>&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu <span class="hljs-number">1500</span>
        inet <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.202</span>  netmask <span class="hljs-number">255.255</span><span class="hljs-number">.255</span><span class="hljs-number">.0</span>  broadcast <span class="hljs-number">192.168</span><span class="hljs-number">.100</span><span class="hljs-number">.255</span>
        inet6 fe80::f056:ea91:f190:c311  prefixlen <span class="hljs-number">64</span>  scopeid <span class="hljs-number">0x20</span>&lt;link&gt;
        ether <span class="hljs-number">00</span>:<span class="hljs-number">0</span>c:<span class="hljs-number">29</span>:e3:<span class="hljs-number">48</span>:<span class="hljs-number">83</span>  txqueuelen <span class="hljs-number">1000</span>  (Ethernet)
        RX packets <span class="hljs-number">1033084</span>  bytes <span class="hljs-number">1166003951</span> (<span class="hljs-number">1.0</span> GiB)
        RX errors <span class="hljs-number">0</span>  dropped <span class="hljs-number">0</span>  overruns <span class="hljs-number">0</span>  frame <span class="hljs-number">0</span>
        TX packets <span class="hljs-number">344264</span>  bytes <span class="hljs-number">37918295</span> (<span class="hljs-number">36.1</span> MiB)
        TX errors <span class="hljs-number">0</span>  dropped <span class="hljs-number">0</span> overruns <span class="hljs-number">0</span>  carrier <span class="hljs-number">0</span>  collisions <span class="hljs-number">0</span>
</code></pre>
<p>映射挂载目录</p>
<pre><code class="hljs autoit"><span class="hljs-meta"># 安装支持docker-machine使用mount命令的工具</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># yum -y install fuse-sshfs rpel-release</span>

<span class="hljs-meta"># 创建要挂载的目录</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># mkdir /lmk</span>

<span class="hljs-meta"># 在host1中创建挂载目录</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine ssh host1 mkdir /kml</span>

<span class="hljs-meta"># 将host1中的kml目录挂载到docker-machine主机的lmk目录</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine mount host1:/kml /lmk</span>

<span class="hljs-meta"># 在本机cyj目录创建index.html文件</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># echo <span class="hljs-string">&quot;The page in the docker-machine&quot;</span> &gt; /lmk/index.html</span>

<span class="hljs-meta"># 查看在host1的挂载目录是否存在</span>
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine ssh host1 ls /kml</span>
index.html
[root<span class="hljs-symbol">@localhost</span> ~]<span class="hljs-meta"># docker-machine ssh host1 cat /kml/index.html</span>
The page <span class="hljs-keyword">in</span> the docker-machine</code></pre>
<ul>
<li>卸载 ： docker-machine mount -u host1:/kml /lmk</li>
</ul>
<p>远程重启</p>
<pre><code class="hljs apache"><span class="hljs-attribute">docker</span>-machine restart host<span class="hljs-number">1</span></code></pre>

</li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/docker/">docker</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/docker/">docker</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章是以学习为目的，如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/12/10/docker-overylay%E7%BD%91%E7%BB%9C/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker-overylay</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/12/08/docker-compose/">
                        <span class="hidden-mobile">docker-compose-docker集群的快速编排</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://space.bilibili.com/39165374" target="_blank" rel="nofollow noopener"><span>LMK</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "docker-machine&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>















</body>
</html>
